package com.yanqu.road.server.logic.activity.condition.recharge.systemopen;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.config.goods.GetRechargeMallResult;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.log.LogTriggerRMBMallBuy;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.server.manger.config.MallRechargeConfigMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;

/**
 * 妙音坊系统开放礼包
 */
public class MusicRoomSystemOpenRechargeTriggerCondition extends BaseActivityCondition {

    public MusicRoomSystemOpenRechargeTriggerCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    public boolean isCompleted(GamePlayer player) {
        return getValue().compareTo(activityConditionInfo.getParamList().get(3)) >= 0;
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.MusicRoom.getValue())) {
            return;
        }
        int args = (int) object;
        if (args > 0) {
            //加道具，设置购买状态
            if (getValue().intValue() == args) {
                // 临时处理热更，后续抽出来
                int mallId = args;
                UserActivityConditionData userActivityConditionData = baseActivityData.getUserActivityConditionData(getConditionId());
                // 这边先处理下每日重置
                long nowTime = System.currentTimeMillis() / 1000;
                if (nowTime > userActivityConditionData.getModifyTime()) {
                    MallInfo mallInfo = MallMgr.getMallInfoByMallId(mallId);
                    String reward = "";
                    if (null == mallInfo) {
                        logger.error("RMBMallActivity mall no find,mallId:{}", mallId);
                    } else {
                        reward = mallInfo.getReward();
                        player.getModule(CurrencyModule.class).addCurrency(mallInfo.getReward(), eLogMoneyType.Activity, eLogMoneyType.RMBMallActivityReward);
                    }
                    // 设置下次可领奖时间
                    userActivityConditionData.setModifyTime(DateHelper.getTodayZeroTime() + DateHelper.DAY_SECONDS);
                    player.getModule(ActivityModule.class).onActivityDataChanged(baseActivityData);
                    LogMgr.addLogActivityReward(player.getUserId(), activityInfo.getActivityId(), activityInfo.getType(), activityInfo.getChildType(),
                            activityConditionInfo.getConditionId(), 0, false, reward);
                    long price = MallRechargeConfigMgr.getPrice(mallInfo);
                    AutoLogMgr.add(new LogTriggerRMBMallBuy(player.getUserId(), activityInfo.getActivityId(), activityConditionInfo.getConditionId(),
                            activityConditionInfo.getType(), mallId, price));
                } else {
                    logger.error("already get reward activityId:{}; conditionId:{}", activityInfo.getActivityId(), getConditionId());
                }
            }
        } else {
            if (getValue().compareTo(BigInteger.ZERO) == 0) {
                GetRechargeMallResult result = player.getModule(ActivityModule.class).getSystemOpenShowRMBMallInfo(activityConditionInfo);
                if (result.isDuringCd() && args != -666) {
                    return;
                }
                setSystemOpenRMBMallActivityInfo(result.getMallInfo());
            }
        }
    }

    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.MusicRoomSystemOpenRechargeTrigger.getValue(), handle);
    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.MusicRoomSystemOpenRechargeTrigger.getValue(), handle);
    }
}
